/**
 * @param {number[][]} grid
 * 思路是按对角线转换成长度从1开始的数组
 * @return {number[][]}
 */
var differenceOfDistinctValues = function(grid) {
    const m = grid.length
    const n = grid[0].length
    const arr = []
    const answer = Array.from({ length: m }, () => new Array(n).fill(0));
    console.log(answer)
    grid.forEach((item, x) => {
        item.forEach((i, y) => {
            const index = (x - y) >= 0 ? x - y : x - y + n + m - 1
            arr?.[index]?.length ? arr[index].push(i) : arr[index] = [i] 
        })
    })
    console.log(arr)
    for(let x=0;x<m;x++) {
        for(let y=0;y<n;y++) {
            const index = (x - y) >= 0 ? x - y : x - y + n + m - 1
            // console.log(x, y, index, arr[index])
            // if (arr[index].length === 1) answer[x][y] = 0
            // else if (arr[index].length === 2) answer[x][y] = 1
            // else {
                const targetIndex = (x - y) >= 0 ? y : x
                const liftSet = new Set(arr[index].slice(0, targetIndex))
                const rightSet = new Set(arr[index].slice(targetIndex + 1))
                answer[x][y] = Math.abs(liftSet.size - rightSet.size)
                console.log(arr[index], targetIndex, liftSet, rightSet, x, y, answer[x][y], answer)
            // }
        }
    }
    console.log(answer)
    console.log(answer[0][0])
};


const grid = [[1,2,3],[3,1,5],[3,2,1]]
differenceOfDistinctValues(grid)